#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

void display(const vector<int> &num)
{
    for(auto &elem : num)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

//以下是关于insert的操作
//Linux下就可以报错
void test1()
{
    vector<int> vec = {1, 2, 3, 4, 5};
    display(vec);
    cout << "容器中元素的个数size() = " << vec.size() << endl; 
    cout << "容器中容量的大小capacity() = " << vec.capacity() << endl; 

    cout << endl << "在vector中执行insert操作" << endl; 
    auto it = vec.begin();
    ++it;
    cout << "迭代器指向的元素是, *it = " << *it << endl;
    vec.insert(it, 100);//在it的前面插入元素100
    display(vec);
    cout << "容器中元素的个数size() = " << vec.size() << endl; 
    cout << "容器中容量的大小capacity() = " << vec.capacity() << endl; 

    cout << endl << endl;
    cout << "迭代器指向的元素是, *it = " << *it << endl;
    vec.insert(it, 111);
}

//以下是关于erase的
//Linux下就可以报错
void test2()
{
    vector<int> vec = {1, 2, 2, 2, 6, 2, 2, 7, 6};
    display(vec);
    cout << "容器中元素的个数size() = " << vec.size() << endl; 
    cout << "容器中容量的大小capacity() = " << vec.capacity() << endl; 

    cout << endl;
    //删除连续重复的元素2
    for(auto it = vec.begin(); it != vec.end(); ++it)
    {
        if(2 == *it)
        {
            it = vec.erase(it);
        }
    }
    display(vec);
}

void test22()
{
    vector<int> vec = {1, 2, 2, 2, 6, 2, 2, 7, 6};
    display(vec);
    cout << "容器中元素的个数size() = " << vec.size() << endl; 
    cout << "容器中容量的大小capacity() = " << vec.capacity() << endl; 

    cout << endl;
    //删除连续重复的元素2
    for(auto it = vec.begin(); it != vec.end(); )
    {
        if(2 == *it)
        {
            it = vec.erase(it);
        }
        else
        {
            ++it;
        }
    }
    display(vec);
}

//以下是clear的
//本代码在Linux下没有问题，但是在vs上崩溃
void test3()
{
    vector<int> vec = {1, 2, 3, 4, 5};
    auto it = vec.begin();
    ++it;
    ++it;
    cout << "迭代器指向的元素是，*it = " << *it << endl;
    display(vec);

    cout << endl << "在vector中执行clear操作" << endl;
    vec.clear();
    display(vec);
    cout << "迭代器指向的元素是，*it = " << *it << endl;//未定义行为
}

//以下是resize与shrink_to_fit
void test4()
{
    vector<int> vec = {1, 2, 3, 4, 5};
    cout << "元素的个数size() = " << vec.size() << endl;//元素个数为5
    auto it = vec.begin() + 2;  //现在,it指向元素3

    vec.resize(2);  // 调整vector的大小，使其只包含前两个元素
    cout << "元素的个数size() = " << vec.size() << endl;//元素个数为2

    //it现在是失效的迭代器，因为它指向的元素已经被删除了。如果我们
    //尝试使用它，将会产生未定义的行为
    //linux下没有问题，但是vs上报错
    cout << *it << endl;  // 未定义的行为！
}

void test5()
{
    vector<int> vec = { 1, 2, 3, 4, 5 };
    cout << "元素的个数size() = " << vec.size() << endl;
    cout << "容量的大小capacity() = " << vec.capacity() << endl;
    display(vec);

    cout << endl;
    vec.push_back(6);
    auto it = vec.begin();
    cout << "*it = " << *it << endl;
    cout << "元素的个数size() = " << vec.size() << endl;
    cout << "容量的大小capacity() = " << vec.capacity() << endl;
    display(vec);

    cout << endl;
    vec.shrink_to_fit();
    cout << "元素的个数size() = " << vec.size() << endl;
    cout << "容量的大小capacity() = " << vec.capacity() << endl;
    cout << "*it = " << *it << endl;//迭代器指向元素无效
    display(vec);
}

int main(int argc, char *argv[])
{
    test5();
    return 0;
}

